<?php

namespace app\admin\controller;

use app\common\Common;
use app\common\BaseHelper;

use think\Request;

class Index extends Base
{

    protected $redirect = '';

    public function _initialize()
    {
        parent::_initialize();

        $this->model = 'organization';
        //$this->indexView = 'index';
        $this->redirect = 'admin/index/index';
        $this->defaultWhere = [
            'villageId' => $this->admin->villageId,
            'isDelete' => ['neq', 1]
        ];
        /*$villagesModel = model('Villages');
        $this->searchFields = [
            'townId' => [
                'label'     => '乡镇',
                'field'     => 'townId',
                'type'      => 'select',
                'disabled'  => false,
                'condition' => '=',
                'data'      => BaseHelper::makeOptions(
                    $villagesModel,
                    ['level' => 4],
                    ['sorting' => 'asc', 'id' => 'asc'],
                    ['0' => '所有乡镇']
                )
            ],
            'villageId' => [
                'label'     => '村子',
                'field'     => 'villageId',
                'type'      => 'select',
                'disabled'  => false,
                'condition' => '=',
                'data'      => BaseHelper::makeOptions(
                    $villagesModel,
                    ['level' => 5],
                    ['sorting' => 'asc', 'id' => 'asc'],
                    ['0' => '所有村子']
                )
            ],
        ];*/
        $this->defaultOrder = 'sorting ASC';
    }

    public function index(){

        return parent::index();
    }

    /**
     * Add
     */
    public function add(Request $request){
        $typeModel = model('Organizationtype');
        $typeIdIds = $typeModel->select();
        $this->assign('typeIdIds', $typeIdIds);

        return parent::add($request);
    }

    /**
     * Add Post
     */
    public function addPost(Request $request, $redirect = ''){
        $redirect = $this->redirect;

        return parent::addPost($request, $redirect);
    }

    /**
     * Edit
     */
    public function edit(Request $request){
        $typeModel = model('Organizationtype');
        $typeIdIds = $typeModel->select();
        $this->assign('typeIdIds', $typeIdIds);

        return parent::edit($request);
    }

    /**
     * Edit Post
     */
    public function editPost(Request $request, $redirect = ''){
        $redirect = $this->redirect;

        return parent::editPost($request, $redirect);
    }

    /**
     * Delete
     */
    public function delete(){
        $model = model($this->model);

        $request = $this->request;
        $id = $request->param('id');
        if(isset($_POST['ids'])){
            // 删除多条
//            $ids = $request->param('ids');
//
//            $result = $model->whereIn('id', $ids)->update(['isDelete' => 1]);
//
//            if($result !== false) {
//                // 职务ID
//                $organizationjob = model('Organizationjob');
//                $jobIds = $organizationjob->whereIn('organizationId', $ids)->field('id')->select();
//                if($jobIds){
//                    $jobIds = array_column($jobIds, 'id');
//                    $organizationjob->whereIn('organizationId', $ids)->update(['isDelete' => 1]);
//                    // 成员&职务 关联
//                    $operatorJob = model('Operatorjob');
//                    $operatorIds = $operatorJob->whereIn('jobId', $jobIds)->field('operatorId')->select();
//                    $operatorIds = array_column($operatorIds, 'operatorId');
//                    $operatorJob->whereIn('jobId', $ids)->update(['isDelete' => 1]);
//                    // 成员
//                    model('Operator')->whereIn('id', $operatorIds)->update(['isDelete' => 1]);
//                }
//
//                $logInfo = $this->admin->name . '删除了' . count($ids) . '条' . $this->model . '数据。';
//                common::adminLog($request, $logInfo);
//            }
        }else if($id){
            //删除一条
            $info = $model->find(['id', $id]);
            if(!$info){
                return redirect('admin/'.$this->model.'/index');
            }

            if($id == $this->admin->organizationId){
                return $this->error('您属于当前组织，您不能删除！', url('admin/index/index'));
            }

            $result = $model->where('id', $id)->update(['isDelete' => 1]);

            if($result !== false) {
                // 职务ID
                $organizationjob = model('Organizationjob');
                $jobIds = $organizationjob->where(['organizationId' => $id, 'isDelete' => 2])->field('id')->select();
                if($jobIds){
                    $jobIds = array_column($jobIds, 'id');
                    $operatorJob = model('Operatorjob');
                    $operator = model('Operator');
                    foreach($jobIds as $jobId){
                        $organizationjob->where('organizationId', $id)->update(['isDelete' => 1]);

                        // 成员&职务 关联
                        $operatorIds = $operatorJob->where(['jobId' => $jobId, 'isDelete' => 2])->field('operatorId')->select();
                        $operatorIds = array_column($operatorIds, 'operatorId');
                        // 成员
                        //$operator->whereIn('id', $operatorIds)->update(['isDelete' => 1]);
                        foreach($operatorIds as $operatorId){
                            // 是否有多个职位
                            $jobCount = $operatorJob->where(['operatorId' => $operatorId, 'isDelete' => 2])->count();
                            if($jobCount == 1){
                                $operator->where('id', $operatorId)->update(['isDelete' => 1]);
                            }
                        }
                        $operatorJob->where(['jobId' => $jobId, 'isDelete' => 2])->update(['isDelete' => 1]);
                    }
                }

                $logInfo = $this->admin->name . '删除了1条' . $this->model . '数据。';
                common::adminLog($request, $logInfo);
            }
        }

        if($result !== false){
            return $this->success('删除成功！', url('admin/index/index'));
        }else{
            return $this->error('删除失败！', url('admin/index/index'));
        }
    }

}